package d0515_Lang包;

public class String类 {
	
	public static void main(String[] args) {
		
		// String 对象的不变特性
		
		
		String a = "hello";
		String b = "hello";
		
		System.out.println(a.equals(b));  // 一定是 true equals 比较内容
		System.out.println(a == b); 	  // String JDK 对其进行了优化 , 常量池缓存
		// == 比较不一定相等
		
		System.out.println(a);
		System.out.println(a.hashCode());
		
		a += " world!";
		
		System.out.println(a);
		System.out.println(a.hashCode());
		
		
		// 如果一个对象是是 new 方式创建, 那么一定是新对象, String 不在常量池中
		String c = new String("hello");
		char[] carr = {'h','e','l','l','o'};
		System.out.println(c);
		c = new String(carr);
		
		System.out.println(a == c);
		System.out.println(b == c);
		
		byte[] barr = {55,55,66,98,67};
		String d = new String(barr);
		System.out.println(d);
		
		System.out.println(c.length());
		
		// substring  不会改变 c 内容
		System.out.println(c.substring(2, 4));
		
		System.out.println(c.length());
		
		// indexOf 查找
		System.out.println(c.indexOf("e"));
		
		// replace 替换
		System.out.println(c.replaceAll("l", "L"));
		
		String str = "张三,李四,王五,赵柳";
		
		// split 切割数组
		String[] names = str.split(",");
		
		for (int i = 0; i < names.length; i++) {
			System.out.println(names[i]);
		}
		// 转大小写
		System.out.println(c.toUpperCase());
		System.out.println(c.toLowerCase());
		
		
		// 可变字符串
		/**
		 * StringBuilder 于 StringBuffer 都是可变字符串
		 * 
		 * StringBuilder  线程不安全, 性能高   没有多线程情况下, 推荐使用
		 * StringBuffer  线程安全, 性能低
		 * 
		 */
		
//		StringBuilder sb = new StringBuilder();
		StringBuffer sb = new StringBuffer();
		//String s = "";
		long startTime = System.currentTimeMillis();
		for( int i=0; i<10000; i++) {
			sb.append(i);
		}
		System.out.println(sb.length());
		long endTime = System.currentTimeMillis();
		System.out.println(endTime - startTime);
		
	}

}
